<template>
  <div align="content">
    <div class="title"><i class="el-icon-document-copy">  最近发布的文章</i></div>
    <div class="message-box">
      <div class="message-contnet-list" @click="goDetail(item.id)">
        <div class="press-row" v-for="(item, i) in list" :key="i">
          <div class="press-body-box">
            <div class="press-title text-ellipsis" @click="goDetail(item.id)">
              {{ item.title }}
            </div>
          </div>
          <div v-if="item.cover" >
            <a  @click="goDetail(item.id)" ><img class="press-cover" :src="`${serveIp}${item.cover}`" alt=""></a>
          </div>
          <div class="press-right-tag" v-if="item.tags.length != 0">
            <span class="tag" v-for="(tag, index) in item.tags" :key="index">
              {{ tag }}
            </span>
          </div>
          <div class="press-time">{{ formatDate(item.publishDate) }} <span  style="margin-left:18px;"> By  <a href="#">{{item.user.nickname}}</a></span></div>
        </div>
      </div>
      <el-empty v-if="list.length === 0" :image-size="200"></el-empty>
      <el-pagination
        v-show="list.length !== 0"
        class="pager"
        small
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="query.pageSize"
        :current-page="query.pageNum"
        @current-change="changePage"
        :page-count="5"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import PressApi from "../../api/press";
import DateFormat from "dateformat";
import CommonApi from "../../api/common";
export default {
  data() {
    return {
      perPage: 3,
      currentPage: 1,
      list: [],
      serveIp:"",
      query:{
        pageNum:1,
        pageSize:8,
        topicId: null,
      },
      total:null,
    };
  },
  methods: {
    goDetail(val) {
      if (val){
        //////本页面打开
        this.$router.push({
          path: "/pressDetail/" + val,
        });
        ////
        
        //////新页面打开
        // let routeUrl = this.$router.resolve({
        //   path: "/pressDetail/" + val,
        // });
        // window.open(routeUrl.href, '_blank');
      }
    },
    changePage(val){
      this.query.pageNum = val
      this.getList()
    },
    getList() {

      PressApi.getList(this.query).then((r) => {
        this.list = [];
        document.documentElement.scrollTop = 0;
        this.total = r.total
        if (r.code === 200) this.list = r.data;
      });
    },
    formatDate(val) {
      return DateFormat(val, "yyyy年 mm月 dd日 HH:MM");
    },
  },
  mounted() {
    CommonApi.getIp().then((r) => {
      this.serveIp = `${r.data}`;
      this.getList();
    });
  },
  //父组件传进来的值
  props: ["topic"],
  //父组件传的值发生改变时监测
  watch: {
    topic: function (val) {
      //重新获取数据
      this.query.topicId = val
      this.query.pageNum = 1
      this.getList();
    },
  },
  computed: {
    rows() {
      return this.items.length;
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  font-size: 16px;
}
.message-box {
  width: 100%;
}

.press-row {
  // background-color: #40A070;
  padding: 0px 24px 18px 24px;
  margin-top: 18px;
  border-bottom: 1px solid #c5c5c5;
  // border-radius: var(--box-radius);
  transition: 0.3s;

  .press-time {
    margin-top: 4px;
    font-size: 12px;
    color: #777777;
  }
  .press-body-box {
    display: flex;
    flex-direction: row;
    flex: 1;
    flex-wrap: wrap;
    .press-title {
      -webkit-line-clamp: 3;
      margin-top: 12px;
      color: #474747;
      font-size: 18px;
      font-weight: 600;
      margin-right: 24px;
      transition: 0.3s;
      &:hover {
        color: var(--font-color-hover);
        cursor: pointer;
      }
    }
  }
  .press-right-tag {
    width: 100%;
    padding: 6px 0px 0px 0px;
    flex-wrap: wrap;
    max-width: 200px;
    background-color: red($color: #000000);
    .tag {
      display: inline-flex;
      font-size: 10px;
      margin: 0px 4px 0px 0px;
      padding: 2px 10px;
      background-color: var(--color3);
      border-radius: var(--box-radius);
      color: #fff;
    }
  }
  .press-cover{
    max-width:100%;
    max-height: 300px;
    object-fit:cover;
    margin:12px 0;
    border-radius: 6px;
  }
}
@media (max-width: 528px) {
  .press-row {
    padding: 0px 0px 18px 0px;
  }
}

.pager {
  margin-top: 24px;
  overflow: auto;
  width: 100%;
}
</style>